package org.example.com.pattern.create.abstract_factory

/**
 * 创建型模式-抽象工厂模式
 * 适用于创建多维度产品的场景。
 */
interface Button {
    fun paint()
}

class WinButton : Button {
    override fun paint() {
        println("Win：绘制Button")
    }
}

class MacButton : Button {
    override fun paint() {
        println("Mac：绘制Button")
    }
}

interface CheckBox {
    fun paint()
}

class WinCheckBox : CheckBox {
    override fun paint() {
        println("Win：绘制CheckBox")
    }
}

class MacCheckBox : CheckBox {
    override fun paint() {
        println("Mac：绘制CheckBox")
    }
}

interface GUIFactory {
    fun createButton(): Button
    fun createCheckBox(): CheckBox
}

class WinFactory : GUIFactory {
    override fun createButton(): Button {
        return WinButton()
    }

    override fun createCheckBox(): CheckBox {
        return WinCheckBox()
    }
}

class MacFactory : GUIFactory {
    override fun createButton(): Button {
        return MacButton()
    }

    override fun createCheckBox(): CheckBox {
        return MacCheckBox()
    }
}

class Application(private val factory: GUIFactory) {
    private var button: Button? = null
    private var checkBox: CheckBox? = null
    fun createUI() {
        button = factory.createButton()
        checkBox = factory.createCheckBox()
    }

    fun paint() {
        button?.paint()
        checkBox?.paint()
    }
}

fun main() {
    val os = "Windows"
    val factory = when (os) {
        "Windows" -> WinFactory()
        "Mac" -> MacFactory()
        else -> throw IllegalArgumentException("error！未知操作系统！")
    }
    val app = Application(factory)
    app.createUI()
    app.paint()
}